package com.qf.oa_system.controller;

import com.qf.oa_system.entity.Student;
import com.qf.oa_system.entity.User;
import com.qf.oa_system.entity.Weekly;
import com.qf.oa_system.service.StudentService;
import com.qf.oa_system.service.WeeklyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * ?ܱ??(Weekly)表控制层
 *
 * @author makejava
 * @since 2020-06-03 20:26:33
 */
@Controller
@RequestMapping("weekly")
public class WeeklyController {
    /**
     * 服务对象
     */
    @Resource
    private WeeklyService weeklyService;

    @Autowired
    StudentService studentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Weekly selectOne(Integer id) {
        return this.weeklyService.queryById(id);
    }

    @GetMapping("deleteweek")
    public String deleteweek(int weekId){
        boolean b = weeklyService.deleteById(weekId);
            return "redirect:/student/toWeek";
    }
    @PostMapping("insertweek")
    public String insertweek(Weekly weekly, HttpSession session,String content){

        User user = (User)session.getAttribute("user");
        Student student = new Student();
        student.setUserId(user.getUserId());
        Student student1 = studentService.queryAllByUserId(user.getUserId());
        Integer stuId = student1.getStuId();

        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        weekly.setWeekTime(date);
        weekly.setWeekContent(content);
        weekly.setStuId(stuId);
//test
        weeklyService.insert(weekly);
        return "redirect:/student/toWeek";
    }
}